package com.sino.androiddesign.customView


import android.content.Context
import android.graphics.*
import android.graphics.drawable.PictureDrawable
import android.os.Bundle
import android.os.Handler
import android.support.v4.app.Fragment
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.sino.androiddesign.R
import kotlinx.android.synthetic.main.fragment_demo3_canvans_bitmap_text.*


/**
 * canvas 图片和文字
 *
 */
class Demo3CanvasBitmapTextFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_demo3_canvans_bitmap_text, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        demo3_btn_check.setOnClickListener {
            demo3_check_view.check()
        }

        demo3_btn_uncheck.setOnClickListener {

            demo3_check_view.uncheck()
        }
    }


}

/**
 *  picture 相对于录制画布操作的录像机
 *  canvas 上面的动作都会被记录到picture
 *
 *  picture 的内容必须手动调用
 *  1: picture.draw(canvas)
 *  2: canvas.drawPicture(picture)
 *  3:将Picture包装成为PictureDrawable，使用PictureDrawable的draw方法绘制(必须设置显示区域 setBounds)
 */

class Demo3TestView : View {

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet) {
        println("constructor")
        recording()
    }

    init {
        println("init")
    }

    private val mPaint = Paint()
    private val mPicture = Picture()


    private fun recording() {
        val canvas = mPicture.beginRecording(500, 500)
        mPaint.color = Color.BLUE
        mPaint.style = Paint.Style.FILL

        canvas.translate(250f, 250f)
        canvas.drawCircle(0f, 0f, 100f, mPaint)
        mPicture.endRecording()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

//        mPicture.draw(canvas)
//        canvas!!.drawPicture(mPicture)

        //必须设置绘制区域
        val picDra = PictureDrawable(mPicture)
        picDra.setBounds(0, 0, 250, mPicture.height)
        picDra.draw(canvas)

    }

}

class CheckView : View {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet) {
        init(context)
    }


    private val ANIM_NULL = 0   //动画状态-没有
    private val ANIM_CHECK = 1  //动画状态-开启
    private val ANIM_UNCHECK = 2 //动画状态-结束

    private var mWidth = 0
    private var mHeight = 0


    private val mPaint = Paint()
    private lateinit var okBitmap: Bitmap
    private lateinit var okBitmap2: Bitmap

    private var animCurrentPage = 2 //当前页码
    private var animMaxPage = 10 //总页码
    private var animDuration = 5000 //动画时长
    private var animState = ANIM_NULL //动画状态

    private var mHandler: Handler? = null

    private var isCheck = false


    private fun init(context: Context) {
        mPaint.color = 0xffFF5317.toInt()
        mPaint.style = Paint.Style.FILL
        mPaint.isAntiAlias = true

        okBitmap = BitmapFactory.decodeResource(resources, R.mipmap.checkmark)
        okBitmap2 = BitmapFactory.decodeResource(resources, R.mipmap.gou)

        mHandler = Handler {

            invalidate()

            if (animCurrentPage in 0..(animMaxPage - 1)){

                println("执行重绘$animState")

                when(animState){
                    ANIM_CHECK->{
                        animCurrentPage +=1

                        println("handler 对当前页码累加 $animCurrentPage")
                        mHandler!!.sendEmptyMessageDelayed(0,500)
                    }
                    ANIM_UNCHECK->{
                        animCurrentPage--
                        mHandler!!.sendEmptyMessageDelayed(0,500)
                    }
                    ANIM_NULL->{

                    }
                }

            }else{
                animCurrentPage = if (isCheck){
                    animMaxPage-1
                }else{
                    -1
                }

                animState = ANIM_NULL
            }
            true
        }
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        mWidth = w
        mHeight = h

    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)


        println("画了图片")
        canvas!!.translate(mWidth / 2.toFloat(), mHeight / 2.toFloat())

//        canvas.drawCircle(0f, 0f, 300f, mPaint)
//
//        val src = Rect(0,0,okBitmap.width,okBitmap.height/2)
//
//        val dst = Rect(0,0,okBitmap.width,okBitmap.height/2)


        /**
         * src 就是要显示的图片区域
         * dst 在屏幕上显示的区域,如果src 大于dst 区域,会缩放
         */

//        canvas.drawBitmap(okBitmap,src,dst,null)

        //图像选区
//

//        val src = Rect(okBitmap.height * animCurrentPage, 0, okBitmap.height * (animCurrentPage + 1), okBitmap.height)
//        val dst = Rect(-200, -200, 200, 200)
//
//
//        canvas.drawBitmap(okBitmap, src, dst, null)

        val fl = animCurrentPage.toFloat() / animMaxPage.toFloat()
        println("绘制的时候 出现的 数字 $fl")



        val srcWidth = (okBitmap2.width * fl).toInt()

        println("展示图片的宽度 $srcWidth => $animCurrentPage => $animMaxPage => ${okBitmap2.width}")
        val src2 = Rect(0, 0, srcWidth, okBitmap2.height)

        val dstRight = srcWidth +(-okBitmap2.width/2)

        val dst2 = Rect(-srcWidth/2, -okBitmap2.height/2, srcWidth/2, okBitmap2.height/2)


        canvas.drawBitmap(okBitmap2, src2, dst2, null)


        println("结束")

    }


    public fun check(){
        if (animState!== ANIM_NULL || isCheck){
            return
        }else{
            animState =ANIM_CHECK
            animCurrentPage =0
            mHandler!!.sendEmptyMessageDelayed(0,500)
            isCheck =true
        }
    }



    public fun uncheck(){
        if (animState!== ANIM_NULL || !isCheck){
            return
        }else{
            animState =ANIM_UNCHECK
            animCurrentPage =animMaxPage-1
            mHandler!!.sendEmptyMessageDelayed(0,animDuration/animMaxPage.toLong())
            isCheck =false
        }
    }

}
